<?php
require_once ("BaseDAO.php");

// Event Value Object
class Event extends BaseVO {
	var $id;
	var $title;
	var $event_type;
	var $event_status;
	var $location_id;
	var $contact_id;
	var $start;
	var $end;
	var $note;
	var $is_public;

	// create a new VO
	function Event(
		$id= 0,
		$title= "",
		$event_type= 0,
		$event_status= 0,
		$location_id= 0,
		$contact_id= 0,
		$start= "",
		$end= "",
		$note= "",
		$is_public= ""
	) {
		$this->id = $id;
		$this->title = $title;
		$this->event_type = $event_type;
		$this->event_status = $event_status;
		$this->location_id = $location_id;
		$this->contact_id = $contact_id;
		$this->start = $start;
		$this->end = $end;
		$this->note = $note;
		$this->is_public = $is_public;
	}

	// compare to another VO
	function equals($vo) {
		return $this->id == $vo->id &&
			$this->title == $vo->title &&
			$this->event_type == $vo->event_type &&
			$this->event_status == $vo->event_status &&
			$this->location_id == $vo->location_id &&
			$this->contact_id == $vo->contact_id &&
			$this->start == $vo->start &&
			$this->end == $vo->end &&
			$this->note == $vo->note &&
			$this->is_public == $vo->is_public;
	}
	// copies another VO
	function copy($vo) {
		$this->id = $vo->id;
		$this->title = $vo->title;
		$this->event_type = $vo->event_type;
		$this->event_status = $vo->event_status;
		$this->location_id = $vo->location_id;
		$this->contact_id = $vo->contact_id;
		$this->start = $vo->start;
		$this->end = $vo->end;
		$this->note = $vo->note;
		$this->is_public = $vo->is_public;
	}

	// output as a string
	function toString() {
		return $this->id . "," . $this->title . "," . $this->event_type . "," . $this->event_status . "," . $this->location_id . "," . $this->contact_id . "," . $this->start . "," . $this->end . "," . $this->note . "," . $this->is_public;
	}

	// output as XML node
	function toXML() {
		return "<row>\n" .
		"<id>$this->id</id>\n" .
		"<title>$this->title</title>\n" .
		"<event_type>$this->event_type</event_type>\n" .
		"<event_status>$this->event_status</event_status>\n" .
		"<location_id>$this->location_id</location_id>\n" .
		"<contact_id>$this->contact_id</contact_id>\n" .
		"<start>$this->start</start>\n" .
		"<end>$this->end</end>\n" .
		"<note>$this->note</note>\n" .
		"<is_public>$this->is_public</is_public>\n" .
		"</row>\n";
	}

	// read from an html form
	function readForm($prefix="") {
		$this->id = $this->formHelper($prefix . "id", 0);
		$this->title = $this->formHelper($prefix . "title", "");
		$this->event_type = $this->formHelper($prefix . "event_type", 0);
		$this->event_status = $this->formHelper($prefix . "event_status", 0);
		$this->location_id = $this->formHelper($prefix . "location_id", 'null');
		$this->contact_id = $this->formHelper($prefix . "contact_id", 'null');
		$this->start = $this->formHelper($prefix . "start", "");
		$this->end = $this->formHelper($prefix . "end", "");
		$this->note = $this->formHelper($prefix . "note", "");
		$this->is_public = $this->formHelper($prefix . "is_public", "");
	}

	// read from the query string
	function readQuery($prefix="") {
		$this->id = $this->queryHelper($prefix . "id", 0);
		$this->title = $this->queryHelper($prefix . "title", "");
		$this->event_type = $this->queryHelper($prefix . "event_type", 0);
		$this->event_status = $this->queryHelper($prefix . "event_status", 0);
		$this->location_id = $this->queryHelper($prefix . "location_id", 'null');
		$this->contact_id = $this->queryHelper($prefix . "contact_id", 'null');
		$this->start = $this->queryHelper($prefix . "start", "");
		$this->end = $this->queryHelper($prefix . "end", "");
		$this->note = $this->queryHelper($prefix . "note", "");
		$this->is_public = $this->queryHelper($prefix . "is_public", "");
	}

	// extra functions here
	function getPK() {
		return $this->id;
	}
}

// Event Data Access Object
class EventDAO extends BaseDAO {
	var $SQL_SELECT = "SELECT * FROM `event` ";
	var $SQL_COUNT = "SELECT count(*) AS cnt FROM `event` ";
	// insert with no value for an auto_increment field.
	var $SQL_INSERT = "INSERT INTO `event` (title,event_type,event_status,location_id,contact_id,start,end,note,is_public) VALUES ('%A',%B,%C,%D,%E,'%F','%G','%H','%I')";
	var $SQL_UPDATE = "UPDATE `event` SET ";
	var $SQL_DELETE = "DELETE FROM `event` WHERE id=%A";

	// default constructor
	function EventDAO($dbserver="", $dbname="", $dbuser="", $dbpass="") {
		// calls the parent constructor which
		// makes the database connection.
		parent::BaseDAO($dbserver, $dbname, $dbuser, $dbpass);
	}

	// returns a single Event obeject
	function get($id) {    
	$this->sql = $this->SQL_SELECT . "WHERE (id='" . $this->safeEscapeStr($id) . "')";
		$this->exec($this->sql);
		if($this->numRows() > 0 ) {
			$row = $this->getObject();
			$vo = new Event(
				$row->id, 
				$row->title, 
				$row->event_type, 
				$row->event_status, 
				$row->location_id, 
				$row->contact_id, 
				$row->start, 
				$row->end, 
				$row->note, 
				$row->is_public
			);
			return $vo;
		} else {
			return null;    
		}
	}

	// returns an array of Event objects
	// given a complete SQL query 
	// The SQL Query needs to be safely escaped by the caller!
	function findBySQL($sql) {
		$voList = array();
		$this->sql = $sql;
		$this->exec($this->sql);
		while($row = $this->getObject()) {
			$vo = new Event(
				$row->id, 
				$row->title, 
				$row->event_type, 
				$row->event_status, 
				$row->location_id, 
				$row->contact_id, 
				$row->start, 
				$row->end, 
				$row->note, 
				$row->is_public
			);
			array_push($voList, $vo);
		}
		return $voList;
	}

	// returns an array of Event objects
	// value for $where is some thing like "(xxx = 'abc') AND (zzz='123')"
	// value for $order_by is a comma separated list of columns "company, name"
	// the where and order clauses should be safely escaped by the caller!
	function findWhere($where = "", $orderby = "") {
		$this->sql = $this->SQL_SELECT;
		$voList = array();
		if(strlen($where) > 0 ) {
			$where = "WHERE (" . $where . ") ";
		}
		if(strlen($orderby) > 0) {
			$orderby = "ORDER BY " . $orderby;
		} else {
			$orderby = "ORDER BY 1";
		}
		$this->sql .= $where . " " . $orderby;
		$this->exec($this->sql);
		while($row = $this->getObject()) {
			$vo = new Event(
				$row->id, 
				$row->title, 
				$row->event_type, 
				$row->event_status, 
				$row->location_id, 
				$row->contact_id, 
				$row->start, 
				$row->end, 
				$row->note, 
				$row->is_public
			);
			array_push($voList, $vo);
		}
		return $voList;
	}

	// returns an array of Event objects based on the example
	function findExample($example, $orderby="") {
		$this->sql = $this->SQL_SELECT;
		$voList = array();
		$where = "";

		$tmp = "";
		if($example->id != 0) $tmp .= "id = " . $this->safeEscapeStr($example->id) . " and ";
		if(strlen($example->title) > 0) $tmp .= "title like '%" . $this->safeEscapeStr($example->title) . "%' and ";
		if($example->event_type != 0) $tmp .= "event_type = " . $this->safeEscapeStr($example->event_type) . " and ";
		if($example->event_status != 0) $tmp .= "event_status = " . $this->safeEscapeStr($example->event_status) . " and ";
		if($example->location_id != 0) $tmp .= "location_id = " . $this->safeEscapeStr($example->location_id) . " and ";
		if($example->contact_id != 0) $tmp .= "contact_id = " . $this->safeEscapeStr($example->contact_id) . " and ";
		if(strlen($example->start) > 0) $tmp .= "start = DATE('" . $this->safeEscapeStr($example->start) . "') and ";
		if(strlen($example->end) > 0) $tmp .= "end = DATE('" . $this->safeEscapeStr($example->end) . "') and ";
		if(strlen($example->note) > 0) $tmp .= "note like '%" . $this->safeEscapeStr($example->note) . "%' and ";
		if(strlen($example->is_public) > 0) $tmp .= "is_public like '%" . $this->safeEscapeStr($example->is_public) . "%' and ";
		if ($tmp != "") {$where = "WHERE (" . substr($tmp, 0, strlen($tmp)-4) . ") ";}
		if(strlen($orderby) > 0) {
			$orderby = "ORDER BY " . $orderby;
		} else {
			$orderby = "ORDER BY 1";
		}
		$this->sql .= $where . " " . $orderby;
		$this->exec($this->sql);
		while($row = $this->getObject()) {
			$vo = new Event(
				$row->id, 
				$row->title, 
				$row->event_type, 
				$row->event_status, 
				$row->location_id, 
				$row->contact_id, 
				$row->start, 
				$row->end, 
				$row->note, 
				$row->is_public
			);
			array_push($voList, $vo);
		}
		return $voList;
	}

	// insert a record from a vo
	function insert(&$vo) {
		$this->sql = $this->SQL_INSERT;
		$this->sql = str_replace("%A", $this->safeEscapeStr($vo->title), $this->sql);
		$this->sql = str_replace("%B", $this->safeEscapeStr($vo->event_type), $this->sql);
		$this->sql = str_replace("%C", $this->safeEscapeStr($vo->event_status), $this->sql);
		$this->sql = str_replace("%D", $this->safeEscapeStr($vo->location_id), $this->sql);
		$this->sql = str_replace("%E", $this->safeEscapeStr($vo->contact_id), $this->sql);
		$this->sql = str_replace("%F", $this->safeEscapeStr($vo->start), $this->sql);
		$this->sql = str_replace("%G", $this->safeEscapeStr($vo->end), $this->sql);
		$this->sql = str_replace("%H", $this->safeEscapeStr($vo->note), $this->sql);
		$this->sql = str_replace("%I", $this->safeEscapeStr($vo->is_public), $this->sql);

		$this->exec($this->sql);
		$vo->id = $this->lastOID();
		return $this->affectedRows();
	}

	// update a record from a vo
	function update($vo) {
		$this->sql = $this->SQL_UPDATE;
		$this->sql .= "title = '" . $this->safeEscapeStr($vo->title) . "', ";
		$this->sql .= "event_type = " . $this->safeEscapeStr($vo->event_type) . ", ";
		$this->sql .= "event_status = " . $this->safeEscapeStr($vo->event_status) . ", ";
		$this->sql .= "location_id = " . $this->safeEscapeStr($vo->location_id) . ", ";
		$this->sql .= "contact_id = " . $this->safeEscapeStr($vo->contact_id) . ", ";
		$this->sql .= "start = '" . $this->safeEscapeStr($vo->start) . "', ";
		$this->sql .= "end = '" . $this->safeEscapeStr($vo->end) . "', ";
		$this->sql .= "note = '" . $this->safeEscapeStr($vo->note) . "', ";
		$this->sql .= "is_public = '" . $this->safeEscapeStr($vo->is_public) . "' ";

		$this->sql .= "WHERE id=" . $this->safeEscapeStr($vo->id);

		$this->exec($this->sql);
		return $this->affectedRows();
	}

	//save an object to the databse (update or insert as needed)
	function save(&$vo) {
		if ($vo->id <= 0)
			return $this->insert($vo);
		else
			return $this->update($vo);
	}

	// delete a record from a vo
	function delete($vo) {
		$this->sql = $this->SQL_DELETE;
		$this->sql = str_replace("%A", $this->safeEscapeStr($vo->id), $this->sql);
		$this->exec($this->sql);
		return $this->affectedRows();
	}

}

